home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / P Examples / TN 2 - Think Pascal < prev   
Encoding:
Text File  |  1994-08-28  |  6.8 KB  |  205 lines  |  [TEXT/ttxt]

  1. ===========================================================================
  2.  
  3. Number 2 - Building Commands Using Think Pascal
  4.  
  5. Revisions:    Aug. 28, 1994
  6.  
  7. nShell(tm) Technical Note
  8.  
  9. ===========================================================================
  10.  
  11. Early releases of the nShell contained sample code for the Think C development environment.  This Note describes how to build nShell commands using Think Pascal.
  12.  
  13. Examples
  14. ========
  15.  
  16. Example projects have been released in a package called "nShell(tm) Think Pascal Examples".  This package contains example projects in Think Pascal, as well as C source code for the new "nShell.lib".
  17.  
  18. The Interface
  19. =============
  20.  
  21. The general interface for nShell commands is described in the "Programmer's Guide to the nShell".  For this Pascal interface, a new library (nShell.lib) was written to translate nShell variables and functions from C into Pascal.  The differences between the Pascal interface and the general one are discussed below:
  22.  
  23. A New Main
  24. ----------
  25.  
  26. The nShell.lib now contains the true "main" of the nShell command.  This main routine sets up the environment and calls a Pascal routine:
  27.  
  28.   procedure theCommand (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
  29.  
  30. This new procedure is the new "main" for Pascal commands.  This routine is responsible for managing the nshc_parms "action" and "result" variables, as well as performing any command-specific tasks.
  31.  
  32. A New nshc_parms
  33. ----------------
  34.  
  35. The data interface for commands has been translated into Pascal, and is defined in the file "nshc.p".  Variables within this structure follow the roles set out in the Programmer's Guide.
  36.  
  37. A New nshc_calls
  38. ----------------
  39.  
  40. A new callback interface has been written for Pascal commands.  Calls are now made through the "nShell.lib".  Definitions for these routines are found in the file "nshc.p".  The nshc_calls pointer may not be used directly, but is passed in each call to the library.
  41.  
  42. Each call requires that the nshc_calls pointer be passed as the first parameter.  To ease typing, a shorter name could be used for the call pointer, or macros could be defined to eliminate it entirely.
  43.  
  44. output to stderr
  45. ================
  46.  
  47. NSH_putchar_err
  48. ---------------
  49.  
  50. procedure NSH_putchar_err (nshc_calls: t_nshc_calls; c: char);
  51.  
  52. This routine prints a single character to the stderr device.
  53.  
  54. NSH_puts_err
  55. ------------
  56.  
  57. procedure NSH_puts_err (nshc_calls: t_nshc_calls; c_string: ptr);
  58.  
  59. This routine prints a c-style, null-terminated, string to stderr.  The string buffer should be defined as: "foo : packed array[x..y] of char".  The call may then be made using the @ operator: "NSH_puts_err( nshc_calls, @foo );
  60.  
  61. NSH_putStr_err
  62. --------------
  63.  
  64. procedure NSH_putStr_err (nshc_calls: t_nshc_calls; s: Str255);
  65.  
  66. This routine prints a Pascal string to the stderr device.
  67.  
  68. output to stdout
  69. ================
  70.  
  71. NSH_putchar
  72. -----------
  73.  
  74. procedure NSH_putchar (nshc_calls: t_nshc_calls; c: char);
  75.  
  76. This routine prints a single character to the stdout device.
  77.  
  78. NSH_puts
  79. --------
  80.  
  81. procedure NSH_puts (nshc_calls: t_nshc_calls; c_string: ptr);
  82.  
  83. This routine prints a c-style, null-terminated, string to stdout.  The string buffer should be defined as: "foo : packed array[x..y] of char".  The call may then be made using the @ operator: "NSH_puts( nshc_calls, @foo );
  84.  
  85. NSH_putStr
  86. ----------
  87.  
  88. procedure NSH_putStr (nshc_calls: t_nshc_calls; s: Str255);
  89.  
  90. This routine prints a Pascal string to the stdout device.
  91.  
  92. input from stdin
  93. ================
  94.  
  95. NSH_getchar
  96. -----------
  97.  
  98. function NSH_getchar (nshc_calls: t_nshc_calls): integer;
  99.  
  100. This routine collects a single character from stdin.  Returns:
  101.  
  102. 0 = No character available
  103. -1 = End of input
  104. others = Next character in queue
  105.  
  106. *** NOTE:  An error in nShell versions prior to v1.0.2 caused a ^D key to be returned directly (04 decimal), and not to be converted to -1. ***
  107.  
  108. NSH_gets
  109. --------
  110.  
  111. function NSH_gets (nshc_calls: t_nshc_calls; c_string: ptr; size: integer): integer;
  112.  
  113. This routine collects a c-style string from stdin.  The string buffer should be defined as: "foo : packed array[x..y] of char".  The call may then be made using the @ operator: "NSH_gets( nshc_calls, @foo, size );". The "size" field defines the maximum length of the input string.  Returns:
  114.  
  115. 0 = No character available
  116. -1 = End of input
  117. others = Length of input string
  118.  
  119. NSH_getStr
  120. ----------
  121.  
  122. function NSH_getStr (nshc_calls: t_nshc_calls; s: Str255): integer;
  123.  
  124. This routine collects a 255 character Pascal-style string from stdin.  Returns:
  125.  
  126. 0 = No character available
  127. -1 = End of input
  128. others = Length of input string
  129.  
  130. variable access functions
  131. =========================
  132.  
  133. NSH_var_set
  134. -----------
  135.  
  136. function NSH_var_set (nshc_calls: t_nshc_calls; name: Str32; value: Str255): integer;
  137.  
  138. Assigns the "value" string to a variable of the given "name".  If a matching variable is not found, a new one will be created.  Returns: 0 = success, -1 = could not create variable.
  139.  
  140. NSH_var_unset
  141. -------------
  142.  
  143. function NSH_var_unset (nshc_calls: t_nshc_calls; name: Str32): integer;
  144.  
  145. Removes the variable of the given "name".  Returns: 0 = found and cleared, 1 = could find create variable.
  146.  
  147. NSH_var_env
  148. -----------
  149.  
  150. function NSH_var_env (nshc_calls: t_nshc_calls; name: Str32; value: Str255): integer;
  151.  
  152. Searches for a variable of the given "name", and returns its "value".  Returns: 0 = success, 1 = could find create variable.
  153.  
  154. path expansion functions
  155. ========================
  156.  
  157. NSH_path_expand
  158. ---------------
  159.  
  160. function NSH_path_expand (nshc_calls: t_nshc_calls; path: Str255): integer;
  161.  
  162. Expands a full or partial "path" in place. Returns: 0 = success or 1 = failure.
  163.  
  164. NSH_path_to_FSSpec
  165. ------------------
  166.  
  167. function NSH_path_to_FSSpec( nshc_calls: t_nshc_calls; path: Str255; spec: FSSpecPtr): integer;
  168.  
  169. Expands a full or partial "path" in place. Returns: 0 = success or 1 = failure.
  170.  
  171. NSH_path_which
  172. --------------
  173.  
  174. function NSH_path_which (nshc_calls: t_nshc_calls; cmd_path: Str255): integer;
  175.  
  176. Follows the command search path, and expands a full or partial "cmd_path" in place. Returns: 0 = success or 1 = failure.
  177.  
  178. dialog functions
  179. ================
  180.  
  181. NSH_notify
  182. ----------
  183.  
  184. procedure NSH_notify (nshc_calls: t_nshc_calls; s: Str255; size: integer);
  185.  
  186. Pop a modal dialog containing the given string.  The dialog may be given one of three sizes: 0 = small, 1 = medium, 2 = large.
  187.  
  188. NSH_ask
  189. -------
  190.  
  191. function NSH_ask (nshc_calls: t_nshc_calls; s: Str255; size: integer): integer;
  192.  
  193. Pop a three button modal dialog containing the given string.  The three buttons are "Yes", "No", and "Cancel".  The dialog may be given one of three sizes: 0 = small, 1 = medium, 2 = large.  Returns: 1 = yes, 2 = no, 3 = cancel.
  194.  
  195. misc
  196. ====
  197.  
  198. NSH_match
  199. ---------
  200.  
  201. function NSH_match (nshc_calls: t_nshc_calls; pattern: Str255; target: Str255): integer;
  202.  
  203. The given "pattern" is compared against the "target" according to shell wildcard rules. Returns: 0 = match, 1 = no match.
  204.  
  205.